/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sisos.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author FLAVIOFAV
 */
public class fabricaConexao {
    //private static final SessionFactory sessionFactory;
    //private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private static SessionFactory sessionFactory = buildSessionFactory();
    
    private static SessionFactory buildSessionFactory(){
		try {
			//Configuration cfg = new Configuration();   colocar assim, caso queira trabalhar com xml 
			AnnotationConfiguration cfg = new AnnotationConfiguration(); //neste momento, o hibernate procura pelo arquivo hibernate.properties.
			cfg.configure("hibernate.cfg.xml");		 //se encontrada, todas as configs serão carregadas.
			return cfg.buildSessionFactory(); //responsável por criar a sessionFactory com base no xml
		} catch (Throwable e) {
			System.out.println("Criação inicial do objeto SessionFactory falhou. Erro: " + e.getMessage());
			throw new ExceptionInInitializerError(e);
		}
	}
    
    /*static{
        try{
            sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        } catch(Throwable t){
            throw new ExceptionInInitializerError(t);
        }
     }*/
    public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
    
    /*public static Session getInstance(){
        Session session = (Session) threadLocal.get();
        session = sessionFactory.openSession();
        threadLocal.set(session);
        return session;
    }*/
}
